﻿
using Love;
using MyGrid.Core;
using MyGrid.Core.HexTypeA.Extensions;
using MyGrid.Core.HexTypeA.Visual;

namespace MyGrid.Test
{
    // -11 5 =>0
    // -19 28 =>1
    // 0 -14 =>0
    internal class Example03 : Scene
    {
        private List<MGVector2I> _list;
        private float _unitSize = 30;
        private float _zoom = 1f;
        private MGVector2F _focusPos = new MGVector2F(-200, -200);

        public Example03()
        {
            _list = new List<MGVector2I>();
            var center = (new MGVector2I(0, 0));

            foreach (var item in center.GetArea(3))
            {
                _list.Add(item);
            }
        }

        public override void Draw()
        {
            DrawGrid();
            DrawHitline();
        }

        private void DrawGrid()
        {
            foreach (var item in _list)
            {
                var i = item.X;
                var j = item.Y;

                var pos = HexTypeALocator
                      .GetVisualPosition(new Core.MGVector2I(i, j), _unitSize, _zoom, _focusPos);

                Graphics.SetColor(Color.Aqua);

                var vertexs = HexTypeAVisual.GetVertexs(pos, _unitSize * _zoom);

                for (int k = 0; k < vertexs.Length; k++)
                {
                    var k2 = (k + 1) % vertexs.Length;
                    var p1 = vertexs[k];
                    var p2 = vertexs[k2];
                    Graphics.Line([p1.X, p1.Y, p2.X, p2.Y]);
                }

                Graphics.Ellipse(DrawMode.Fill, pos.X, pos.Y, 2, 2);
            }
        }

        private void DrawHitline()
        {
            var pos = Mouse.GetPosition();

            var originInputPos = new MGVector2F(pos.X, pos.Y );

            var posId = HexTypeAHitTester.ConvertPosToGridId(originInputPos, _unitSize * _zoom, _focusPos);

            var gridCenter = HexTypeALocator.GetVisualPosition(new Core.MGVector2I(posId.X, posId.Y), _unitSize, _zoom, _focusPos);
            Graphics.SetColor(Color.LightGreen);
            Graphics.Line(0, gridCenter.Y, 1000, gridCenter.Y);
            Graphics.Line(gridCenter.X, 0, gridCenter.X, 1000);
            Graphics.Print($"{originInputPos.X} {originInputPos.Y} {posId.X} {posId.Y}");
        }

        static void Main(string[] args)
        {
            Boot.Run(new Example03(), new BootConfig() { WindowX = -1000, WindowY = 100, WindowBorderless = false });
        }
    }
}
